package cn.wowjoy.pda.android_pharmacy.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ItemLrDoorMergeIndexBinding;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.DoorMergeEntity;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.MergeVORequest;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.merge.DoorMergePresenter;

/**
 * Created by Administrator on 2018/4/12.
 */

public class DoorMergeIndexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private LayoutInflater inflater;
    private ItemLrDoorMergeIndexBinding bindingDetail;

    protected Object mItemEventHandler;
    public List<DoorMergeEntity> datas;

    public List<String> DJJLIDS;

    public MergeVORequest mMergeVORequest;

    private String keyWord;
    private String mToken;

    public DoorMergeIndexAdapter(Context context) {
        this.context = context;
    }

    public void setmItemEventHandler(Object mItemEventHandler) {
        this.mItemEventHandler = mItemEventHandler;
    }

    public void addDate(DoorMergeEntity recordResponse){
        if (null == this.datas)
            this.datas = new ArrayList<>();
       if(null  == mMergeVORequest){
           mMergeVORequest = new MergeVORequest();
       }
       if(null == DJJLIDS){
           DJJLIDS = new ArrayList<>();
       }
       if(datas.contains(recordResponse)){
           new MyToast(context).showinfo("当前单子已在合并单中,请勿重复添加");
           return;
       }
        mMergeVORequest.addIds(recordResponse.getResults().getPrescription().getPrescriptionId());
       if(null == mMergeVORequest.getPatientId() || "".equals(mMergeVORequest.getPatientId())){
           mMergeVORequest.setPatientId(recordResponse.getResults().getPrescription().getPatientId());
       }
        if(null == mMergeVORequest.getOperationId() || "".equals(mMergeVORequest.getOperationId())){
//            mMergeVORequest.setZZZGGH(PreferenceManager.getInstance().getLUserNum());
            mMergeVORequest.setOperationId("00026");
        }
        if(null != recordResponse.getResults().getMedicines().get(0).getDjjlid()){
            DJJLIDS.add(recordResponse.getResults().getMedicines().get(0).getDjjlid());
        }

        this.datas.add(0,recordResponse);
//        if (!isContian(recordResponse))
//            saveList.add(recordResponse);
        notifyDataSetChanged();
//        notifyItemChanged(0);
    }
    public void deleteDate(DoorMergeEntity data){
        if (null == datas || datas.size() == 0){
           return;
        }
        datas.remove(data);
        notifyDataSetChanged();
    }

//    public void notifyRealStock(int position,String count){
//        DecimalFormat fnum  =   new  DecimalFormat("##0.00");
//        datas.get(position).setFactOut(Float.parseFloat(fnum.format(Float.parseFloat(count))));
//        notifyDataSetChanged();
//    }
    public void changeItem(DoorMergeEntity item){
        if(null != datas){
            int  position = datas.indexOf(item);
          //  datas.get(position).setFactOut(datas.get(position).getFactOut()+item.getFactOut());
            datas.remove(position);
            datas.add(position,item);
            notifyDataSetChanged();
        }

    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (null == inflater) {
            context = parent.getContext();
            inflater = LayoutInflater.from(context);
        }

        bindingDetail = DataBindingUtil.inflate(inflater, R.layout.item_lr_door_merge_index, parent, false);
        return new StockItemViewHolder(bindingDetail);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        final DoorMergeEntity item = datas.get(position);
       ((StockItemViewHolder) holder).bindData(item,mItemEventHandler);
       ((StockItemViewHolder) holder).binding.rlDetail.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               ((DoorMergePresenter)mItemEventHandler).jump(item);
           }
       });
    }


    @Override
    public int getItemCount() {
        if(null != datas){
            Log.e("PXY", "getItemCount: "+ datas.size());
        }
          return null == datas ? 0 :datas.size();
    }

    public static class StockItemViewHolder extends RecyclerView.ViewHolder {
        ItemLrDoorMergeIndexBinding binding;
        public StockItemViewHolder(ItemLrDoorMergeIndexBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
//            ((SwipeMenuView)itemView).setIos(false).setLeftSwipe(true);
        }

        public void bindData(DoorMergeEntity response, Object mItemEventHandler){
            binding.setPresenter((DoorMergePresenter) mItemEventHandler);
            binding.setModel(response);
            binding.executePendingBindings();
        }
    }

}
